{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.common import PayReceive, Currency, DayCountFraction, BusinessDayConvention\n",
    "from gs_quant.target.common import SwapClearingHouse\n",
    "from gs_quant.instrument import IRSwap\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from datetime import date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Typing question mark after any object, function or method will return docstring\n",
    "IRSwap?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get list of properties of an interest rate swap\n",
    "IRSwap.properties()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swaps = Portfolio()\n",
    "\n",
    "# you don't need to specify any parameters to get a valid trade.  All properties have defaults\n",
    "swaps.append(IRSwap())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Enums - PayReceive"
    ]
   },
   "outputs": [],
   "source": [
    "# pay_or_receive can be a string of 'pay' or 'receive' or the PayReceive enum\n",
    "# and relates to paying or receiving the fixed leg.  defaults to a receiver swap\n",
    "swaps.append(IRSwap(pay_or_receive=PayReceive.Pay))\n",
    "swaps.append(IRSwap(pay_or_receive='Pay'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# termination_date is the end date of the swap.  It may be a tenor relative to effective_date or a datetime.date.\n",
    "# defaults to 10y\n",
    "swaps.append(IRSwap(termination_date=date(2025, 11, 12)))\n",
    "swaps.append(IRSwap(termination_date='1y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Enums - Currency"
    ]
   },
   "outputs": [],
   "source": [
    "# notional currency may be a string or the Currency enum.  defaults to USD\n",
    "swaps.append(IRSwap(notional_currency=Currency.USD))\n",
    "swaps.append(IRSwap(notional_currency='EUR'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the effective date is the start date of the swap and may be a tenor relative\n",
    "# to the active PricingContext.pricing_date or a datetime.date, default is pricing date\n",
    "swaps.append(IRSwap(effective_date='1y'))\n",
    "swaps.append(IRSwap(effective_date=date(2019, 11, 12)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Properties - Solving - Swap fixed rate"
    ]
   },
   "outputs": [],
   "source": [
    "# fixed_rate is the interest rate on the fixed leg of the swap.  Defaults to Par Rate (ATM).\n",
    "# Can be expressed as 'ATM', 'ATM+25' for 25bp above par, a-100 for 100bp below par, 0.01 for 1%, can also be solved for a PV\n",
    "swaps.append(IRSwap(fixed_rate='ATM'))\n",
    "swaps.append(IRSwap(fixed_rate='ATM+50'))\n",
    "swaps.append(IRSwap(fixed_rate='a-100'))\n",
    "swaps.append(IRSwap(fixed_rate=0.01))\n",
    "swaps.append(IRSwap(fixed_rate='10000/pv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# floating_rate_for_the_initial_calculation_period sets the first fixing on the trade.\n",
    "# It should be a float in absolute terms so 0.0075 is 75bp. Defaults to the value derived from fwd curve\n",
    "swaps.append(IRSwap(floating_rate_for_the_initial_calculation_period=0.0075))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Properties - floating_rate_option"
    ]
   },
   "outputs": [],
   "source": [
    "# floating rate option is the index that is being observed, defaults to LIBOR style index for each ccy,\n",
    "# 'OIS' will give the default overnight index for the notional ccy\n",
    "swaps.append(IRSwap(notional_currency='USD', floating_rate_option='USD-ISDA-SWAP RATE'))\n",
    "swaps.append(IRSwap(notional_currency='USD', floating_rate_option='USD-LIBOR-BBA'))\n",
    "swaps.append(IRSwap(notional_currency='EUR', floating_rate_option='EUR-EONIA-OIS-COMPOUND'))\n",
    "swaps.append(IRSwap(notional_currency='GBP', floating_rate_option='OIS'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# floating_rate_designated_maturity is the index term.  defaults to the frequency of the floating leg\n",
    "swaps.append(IRSwap(notional_currency='GBP', floating_rate_designated_maturity='3m'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# floating_rate_spread is a float spread over the index. eg. pay euribor + 1%. defaults to 0\n",
    "swaps.append(IRSwap(pay_or_receive='receive', notional_currency='EUR', floating_rate_spread=0.01))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# floating_rate_frequency is the accrual frequency of the floating leg defined as a tenor.\n",
    "# It will drive the floating_rate_designated_maturity if that has not been independently set.\n",
    "# Defaults to ccy/tenor market standard defaults\n",
    "swaps.append(IRSwap(floating_rate_frequency='6m'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Enums - DayCountFraction"
    ]
   },
   "outputs": [],
   "source": [
    "# floating_rate_day_count_fraction can be the enum used here or a string.  defaults to ccy market standard defaults\n",
    "swaps.append(IRSwap(floating_rate_day_count_fraction=DayCountFraction.ACT_OVER_365L_ISDA))\n",
    "swaps.append(IRSwap(floating_rate_day_count_fraction=DayCountFraction._30E_OVER_360))\n",
    "swaps.append(IRSwap(floating_rate_day_count_fraction='30/360'))\n",
    "swaps.append(IRSwap(floating_rate_day_count_fraction='ACT/360'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Enums - BusinessDayConvention"
    ]
   },
   "outputs": [],
   "source": [
    "# floating_rate_business_day_convention can be the enum used here a the equivalent string\n",
    "swaps.append(IRSwap(floating_rate_business_day_convention=BusinessDayConvention.Following))\n",
    "swaps.append(IRSwap(floating_rate_business_day_convention='Modified Following'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fee is an amount paid.  A positive fee will have a negative impact on the PV.  Defaults to 0\n",
    "swaps.append(IRSwap(fee=50000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# you can specify fee currency and fee date.  trades where the fee is paid in a different currency to the\n",
    "# notional currency are supported.  Default fee currency is notional currency\n",
    "# fee date can be a datetime.date or a tenor. Default fee date is spot dates from the PricingContext.pricing_date\n",
    "swaps.append(IRSwap(notional_currency=Currency.GBP, fee=50000, fee_currency=Currency.GBP, fee_payment_date='1y'))\n",
    "swaps.append(\n",
    "    IRSwap(notional_currency=Currency.GBP, fee=1e5, fee_currency=Currency.USD, fee_payment_date=date(2020, 1, 30))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Enums - SwapClearingHouse"
    ]
   },
   "outputs": [],
   "source": [
    "# valid clearinghouses are held in the SwapClearingHouse enum\n",
    "swaps.append(IRSwap(clearing_house=SwapClearingHouse.LCH))\n",
    "swaps.append(IRSwap(clearing_house=SwapClearingHouse.EUREX))\n",
    "swaps.append(IRSwap(clearing_house='CME'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# you can specify a name for a trade.  This has no economic effect but is useful when extracting results\n",
    "# from a portfolio object\n",
    "swaps.append(IRSwap(PayReceive.Receive, '5y', 'gbp', name='GBP5y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swaps.price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# you can express a swap as a dictionary\n",
    "swap = IRSwap(termination_date='10y', notional_currency='EUR', fixed_rate='ATM+50')\n",
    "swap_dict = swap.as_dict()\n",
    "swap_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Instrument - create from dictionary"
    ]
   },
   "outputs": [],
   "source": [
    "# and you can construct a swap from a dictionary\n",
    "new_swap = IRSwap.from_dict(swap_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swap = IRSwap(effective_date='1y')\n",
    "swap.resolve()\n",
    "swap.as_dict()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
